public class Test21 {
    public int maxProduct(int[] nums) {
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];
        f[0] = g[0] = nums[0];
        int maxNum = f[0];
        for(int i = 1;i < n;i++) {
            if(nums[i] >= 0) {
                f[i] = Math.max(nums[i],f[i - 1]*nums[i]);
                g[i] = Math.min(nums[i],g[i - 1]*nums[i]);
            } else {
                f[i] = Math.max(nums[i],g[i - 1] * nums[i]);
                g[i] = Math.min(nums[i],f[i - 1] * nums[i]);
            }
            maxNum = Math.max(maxNum,f[i]);
        }
        return maxNum;
    }
}
